package unittests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import sgi.details_new.ImageDetails;
import sgi.user.Customer;

public class PurchaseSample {
	
	private List<Customer> customers = new ArrayList<Customer>();
	private List<ImageDetails> images = new ArrayList<ImageDetails>();
	private double imagePrice = 10;
	
	public PurchaseSample()
	{
		images.add(new ImageDetails(10,false));
		images.add(new ImageDetails(20,true));
		images.add(new ImageDetails(30,true));
		customers.add(new Customer("Almog",1000));
		customers.add(new Customer("Ofri",0));
	}
	
	public boolean purchase(String username,int imageID)
	{
		Iterator<Customer> customer = customers.iterator();
		Iterator<ImageDetails> image = images.iterator();
		Customer CurrCustomer;
		ImageDetails CurrImage;
		
		while(customer.hasNext())
		{
			CurrCustomer = customer.next();
			if(CurrCustomer.getUsername().equals(username))
			{
				if(CurrCustomer.getBalance() < imagePrice*0.04)
					return false;
				while(image.hasNext())
				{
					CurrImage = image.next();
					if(CurrImage.getImageid() == imageID)
					{
						if(CurrImage.isHasSample() == true)
						{
							Iterator<Integer> it = CurrCustomer.getSamples().iterator();
							while(it.hasNext())
							{
								if(it.next() == imageID)
									return false;
							}				
							
							CurrCustomer.setBalance((float) (CurrCustomer.getBalance() - imagePrice*0.04));
							CurrCustomer.getSamples().add(imageID);
							return true;
							
						}
						else
							return false;	
					}
				}
			}
		}
		
		return false;
		
	}

}
